package com.google.code.head2head.gwtmodules.server;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.google.code.head2head.gwtmodules.client.services.UserInfoService;
import com.google.code.head2head.model.Entry;
import com.google.code.head2head.service.EntryService;
import com.google.code.head2head.service.LoginService;
import com.google.code.head2head.service.impl.EntryServiceImpl;
import com.google.code.head2head.service.impl.LoginServiceImpl;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class UserInfoServiceImpl extends RemoteServiceServlet implements UserInfoService {

	private EntryService entryService=new EntryServiceImpl();
	private LoginService loginService=new LoginServiceImpl();
	
	
	public List getAllEntries() {
		
		List<Entry> allEntries=entryService.findAll();
		List allEntriesDto=new ArrayList();
		for (Entry entry: allEntries) {
			allEntriesDto.add(entry.transformToDto());
		}
		
		return allEntriesDto;
	}

	public boolean login(String userLogin, String userPassword) {
		return loginService.authenticate(userLogin, userPassword);
	}

	public String createNewEntry(String ip, Double userId, String description,
			String title, String language, Date date, Set tags) {
		
		//creation of the entry object
		Entry entry=new Entry();
		Set ips=new HashSet();
		ips.add(ip);
		entry.setIps(ips);
		entry.setDate(date);
		entry.setDescription(description);
		entry.setLanguage(language);
		entry.setTags(tags);
		
		//saves the entry object
		entryService.create(entry);
		
		//returns successful message
		return "New entry created";
	}

}
